<template>
  <div>
    <a-button type="primary" @click="showModal">
      智能输入
    </a-button>
    <a-modal
      title="输入文本"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      :okText="'确定'"
      :cancelText="'取消'"
    >
      <a-textarea v-model="inputText" :rows="4" placeholder="请输入内容" :autoSize="{ minRows: 4, maxRows: 8 }" />
    </a-modal>
  </div>
</template>

<script>
import { Modal, Button, Input } from 'ant-design-vue'

export default {
  name: 'smartInput',
  components: {
    'a-modal': Modal,
    'a-button': Button,
    'a-textarea': Input.TextArea,
  },
  data() {
    return {
      visible: false,
      inputText: '',
    }
  },
  methods: {
    showModal() {
      this.visible = true
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      const result = this.parseInput(this.inputText)
      console.log(result)
      this.$emit('input-processed', result)
      this.visible = false
      this.inputText = ''
    },
    parseInput(text) {
      // 使用换行符分割文本，然后过滤掉空行
      const lines = text.split('\n').filter(line => line.trim() !== '')

      // 对每一行further分割成单词数组
      return lines.map(line => line.split(/[\t\r]/).filter(word => word !== ''))
    },
  },
}
</script>
